package com.dhcc.bpm.modules.scf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.modules.scf.dto.ScfRenewableInInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfRenewableInInfo;
import com.dhcc.bpm.modules.scf.service.IScfRenewableInInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfRenewableInInfoVO.ScfGetRenewableInDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfRenewableInInfoVO.ScfGetRenewableInListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 再生资源入库控制器
 */
@RestController
@AllArgsConstructor
public class ScfRenewableInInfoController extends BladeController {

    @Autowired
    private IScfRenewableInInfoService scfRenewableInInfoService;

    /**
     * 查询再生资源入库列表
     *
     * @param scfRenewableInInfo
     * @param query
     * @return
     */
    @GetMapping("/scfGetRenewableInList")
    // @Cacheable(value = "scfRenewableInListCache", key = "'scfRenewableInListCacheKey-' + #scfRenewableInInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetRenewableInListVO>> pageList(ScfRenewableInInfoDTO scfRenewableInInfo, Query query) {
        if (StringUtils.isEmpty( scfRenewableInInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfRenewableInInfo.setOrgCodes(  scfRenewableInInfo.getOrgCode().split(",") );


        if(scfRenewableInInfo.getOrgCode().contains( "50000000")){
            scfRenewableInInfo.setOrgCode("50000000");
            scfRenewableInInfo.setOrgCodes("50000000".split(",") );
        }

        return R.data(scfRenewableInInfoService.selectScfRenewableInInfoPage(Condition.getPage(query), scfRenewableInInfo));
    }

    /**
     * 查询再生资源入库单详情
     *
     * @param scfRenewableInInfo
     * @return
     */
    @GetMapping("/scfGetRenewableInDetails")
    // @Cacheable(value = "scfRenewableInDetailsCache", key = "'scfRenewableInDetailsCacheKey-' + #scfRenewableInInfo.rowId")
    public R<ScfGetRenewableInDetailsVO> details(ScfRenewableInInfoDTO scfRenewableInInfo) {
        return R.data(scfRenewableInInfoService.getDetails(scfRenewableInInfo));
    }

    /**
     * 新增再生资源入库信息
     *
     * @param scfRenewableInInfo
     * @return
     */
    @PostMapping("scfAddRenewableInDetails")
    // @CacheEvict(value = "scfRenewableInListCache", allEntries = true)
    public R<Boolean> saveWarehouseEnt(@RequestBody ScfRenewableInInfoDTO scfRenewableInInfo) throws Exception {
        return R.status(scfRenewableInInfoService.saveRemewableInInfo(scfRenewableInInfo));
    }

    /**
     * 修改再生资源入库信息
     *
     * @param scfRenewableInInfo
     * @return
     */
    @PostMapping("/scfModRenewableInDetails")
    // @CacheEvict(value = "scfRenewableInDetailsCache", key = "'scfRenewableInDetailsCacheKey-' + #scfRenewableInInfo.rowId")
    public R<Boolean> updateWarehouseEnt(@RequestBody ScfRenewableInInfoDTO scfRenewableInInfo) {
        return R.status(scfRenewableInInfoService.updateRemewableInInfo(scfRenewableInInfo));
    }

    /**
     * 批量导入再生资源入库信息
     *
     * @param operatorId 上传用户的id
     * @param file       文件
     * @return
     * @throws IOException
     */
    @PostMapping("/scfRenewableInFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> uploadFile(@RequestParam("operatorId") String operatorId, MultipartFile file) throws Exception {
        // 获取到批量导入的数据集合
        List<ScfRenewableInInfo> scfRenewableInInfos = scfRenewableInInfoService.batchSaveRenewableInInfo(operatorId, file);
        int bachSaveRemewableInInfo = scfRenewableInInfoService.bachSaveRemewableInInfo(scfRenewableInInfos, operatorId);
        int total = scfRenewableInInfos.size();
        int totalSussess = bachSaveRemewableInInfo;
        int totalFail = total - totalSussess;
        String msg = "解析到" + total + "条数据，" + "导入成功" + totalSussess + "条，导入失败" + totalFail + "条！";
        return R.success(msg);
    }



    @PostMapping("/scfRenewableInInfo/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        String[] split = ids.split(",");
        return R.status(scfRenewableInInfoService.deleteByIds( split ));
    }


}
